| Conditions | 20 |
| Paths | 255 |
| Total Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like common.js ➔ ... ➔ parse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // Copyright © 2017 Tangdongxin |
||
| 123 | function parse(str, re) { |
||
| 124 | str = reconvert(str); |
||
| 125 | var match, val, tmp, i = 0; |
||
| 126 | var len = str.length; |
||
| 127 | try { |
||
| 128 | for (; match = re.exec(str);) { |
||
| 129 | val = match[0]; |
||
| 130 | if (val == "{" || val == "[") { |
||
| 131 | path.push(node); |
||
| 132 | node.appendChild(cache[val].cloneNode(true)); |
||
| 133 | node = node.lastChild.previousSibling; |
||
| 134 | node.len = 1; |
||
| 135 | node.start = re.lastIndex; |
||
| 136 | } else if ((val == "}" || val == "]") && node.len) { |
||
| 137 | if (node.childNodes.length) { |
||
| 138 | tmp = info.cloneNode(); |
||
| 139 | var content = node.len + ( |
||
| 140 | node.len == 1 ? |
||
| 141 | (val == "]" ? " item, " : " property, ") : |
||
| 142 | (val == "]" ? " items, " : " properties, ") |
||
| 143 | ) + units(re.lastIndex - node.start + 1); |
||
| 144 | |||
| 145 | if (hideDetails) { |
||
| 146 | tmp.setAttribute("title", content); |
||
| 147 | } else { |
||
| 148 | tmp.dataset.content = content; |
||
| 149 | } |
||
| 150 | |||
| 151 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 152 | tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, "\\'")); |
||
| 153 | } |
||
| 154 | node.parentNode.insertBefore(tmp, node); |
||
| 155 | } else { |
||
| 156 | node.parentNode.removeChild(node); |
||
| 157 | } |
||
| 158 | node = path.pop(); |
||
| 159 | } else if (val == ",") { |
||
| 160 | node.len += 1; |
||
| 161 | node.appendChild(comma.cloneNode(true)); |
||
| 162 | } else { |
||
| 163 | tmp = span.cloneNode(); |
||
| 164 | tmp.textContent = match[1] || val; |
||
| 165 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL); |
||
| 166 | node.appendChild(tmp); |
||
| 167 | if (match[3]) { |
||
| 168 | node.appendChild(colon.cloneNode()); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | document.title = ""; |
||
| 173 | JSON.parse(str); |
||
| 174 | } catch (e) { |
||
| 175 | dlog(e); |
||
| 176 | // TODO: find a better way to report error |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 193 |